const logList = []

var _changeHandler = null
var _changeState = null
var _changeBluetoothState = null

const MAX_LENGTH = 100

export const changeState = (state) => {
	if(_changeState){
		_changeState(state)
	}
}

/**
 * 变幻蓝牙状态
 * @param {*} isConnected 
 */
export const changeBlueState = (isConnected) => {
	if(_changeBluetoothState){
		_changeBluetoothState(isConnected)
	}
}

/**
 * 输出
 */
export const log = (text, object) =>{
	if(logList.length > MAX_LENGTH){
		logList.splice(0, 1)
	}
	var displayString = text
	if(object){
		displayString += JSON.stringify(object)
	}
	logList.push(displayString)
	console.log(displayString)
	if(_changeHandler){
		_changeHandler(logList)
	}
}

/**
 * 注册
 */
export const listen = (onChangeCallBack, onChangeStateCallBack, onBlueStateChangeCallBack) =>{
	_changeHandler = onChangeCallBack
	_changeState = onChangeStateCallBack
	_changeBluetoothState = onBlueStateChangeCallBack
}